package com.qinge.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qinge.blog.dao.BlogMessageDao;
import com.qinge.blog.entity.BlogMessage;
import com.qinge.blog.entity.BlogMessageExample;
import com.qinge.blog.utils.CurrentUserUtil;
import com.qinge.blog.vo.paging.PagingReqVO;
import org.springframework.stereotype.Service;
import com.qinge.blog.service.BlogMessageService;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author QDW
 * @date 2022-05-02 23:38:51
 * @description BlogMessage service层实现类
 */
@Service
public class BlogMessageServiceImpl   implements BlogMessageService {
    @Resource
    BlogMessageDao messageDao;

    @Override
    public void add(BlogMessage... blogMessages) {
        for(BlogMessage message:blogMessages){
            message.setUserId(CurrentUserUtil.getUserId());
        }

        messageDao.batchInsert(Arrays.asList(blogMessages));
    }

    @Override
    public void addByList(List<BlogMessage> blogMessages) {
        for(BlogMessage message:blogMessages){
            message.setUserId(CurrentUserUtil.getUserId());
        }

        messageDao.batchInsert(blogMessages);
    }

    @Override
    public void removeById(String id) {
        messageDao.deleteByPrimaryKey(id);
    }

    @Override
    public void removeBatchByIds(List<String> ids) {
        BlogMessageExample example=new BlogMessageExample();
        BlogMessageExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        messageDao.deleteByExample(example);
    }

    @Override
    public void update(BlogMessage blogMessage) {
        messageDao.upsert(blogMessage);
    }

    @Override
    public void upsert(BlogMessage blogMessage) {
        messageDao.upsert(blogMessage);
    }

    @Override
    public BlogMessage findById(String id) {
        return messageDao.selectByPrimaryKey(id);
    }

    @Override
    public List<BlogMessage> findByIds(List<String> ids) {
        BlogMessageExample example=new BlogMessageExample();
        BlogMessageExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        return messageDao.selectByExample(example);
    }

    @Override
    public PageInfo<BlogMessage> findAll() {
        return paging(new PagingReqVO<BlogMessage>());
    }

    @Override
    public PageInfo<BlogMessage> findAll(PagingReqVO<BlogMessage> pagingReqVO) {
        return paging(pagingReqVO);
    }

    @Override
    public PageInfo<BlogMessage> paging(PagingReqVO<BlogMessage> pagingReqVO) {
        PageHelper.startPage(pagingReqVO);

        BlogMessageExample example=new BlogMessageExample();

        List<BlogMessage> list=messageDao.selectByExample(example);

        return new PageInfo<>(list);
    }


}

